package com.hqyj.javaSpringBoot.modules.test.controller;

import com.github.pagehelper.PageInfo;
import com.hqyj.javaSpringBoot.modules.common.entity.Result;
import com.hqyj.javaSpringBoot.modules.common.entity.SearchBean;
import com.hqyj.javaSpringBoot.modules.test.entity.City;
import com.hqyj.javaSpringBoot.modules.test.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class CityController {
    @Autowired
    private CityService cityService;

    /**
     *127.0.0.1:8081/api/city----post
     * {}
     * @param city
     * @return
     */
    @PostMapping(value = "/city",consumes = "application/json")
    public Result<City> insertCity(@RequestBody City city){
        return cityService.insertCity(city);
    }

    /**
     * 127.0.0.1:8081/api/city-----put
     * @param city
     * @return
     */
    @PutMapping(value = "/city",consumes = "application/x-www-form-urlencoded")
    public Result<City> updateCity(@ModelAttribute City city){
        return cityService.updateCity(city);
    }

    /**
     * 127.0.0.1:8081/api/city/2255-----delete
     * @param cityId
     * @return
     */
    @DeleteMapping(value = "/city/{cityId}")
    public Result<Object> deleteCity(@PathVariable int cityId){
        return cityService.deleteCityById(cityId);
    }

    /**
     * 127.0.0.1:8081/api/city/2258-----get
     * @param cityId
     * @return
     */
    @GetMapping("/city/{cityId}")
    public City getCityById(@PathVariable int cityId) {
        return cityService.getCityById(cityId);
    }

    /**
     * 127.0.0.1:8081/api/cities/522-----get
     * @param countryId
     * @return
     */
    @GetMapping("/cities/{countryId}")
    public List<City> getCitiesByCountryId(@PathVariable int countryId) {
        return cityService.getCitiesByCountryId(countryId);
    }

    /**
     * 127.0.0.1:8081/api/cities-----post
     * @param searchBean
     * @return
     */
    @PostMapping(value = "cities",consumes ="application/json" )
    public PageInfo<City> getCitiesBySearchBean(@RequestBody SearchBean searchBean) {
        return cityService.getCitiesBySearchBean(searchBean);
    }

    /**
     * 127.0.0.1:8081/api/batch/cities-----post
     *
     * @param cities
     * @return
     */
    @PostMapping(value = "/batch/cities",consumes = "application/json")
    public Result<Object> insertCities(@RequestBody List<City> cities) {
        return cityService.insertCities(cities);
    }
}
